package org.apache.dubbo.samples.resilience4j.filter;

import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadConfig;
import io.github.resilience4j.bulkhead.BulkheadRegistry;

import io.vavr.CheckedFunction0;
import org.apache.dubbo.rpc.*;


public class Resilience4jBulkheadFilter implements Filter {
    private static Bulkhead bulkhead;
    static {
        BulkheadConfig config = BulkheadConfig.custom()
                .maxConcurrentCalls(150)
                .maxWaitTime(500)
                .build();

        BulkheadRegistry registry = BulkheadRegistry.of(config);

        //Bulkhead bulkheadWithDefaultConfig = registry.bulkhead("name1");

        bulkhead = registry.bulkhead("myBulkhead", config);
    }
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        CheckedFunction0<String> decoratedSupplier = Bulkhead
                .decorateCheckedSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
        Result result = invoker.invoke(invocation);
        return result;
    }
}
